package com.shaw.workrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.FeedRecord;
import com.shaw.workrecord.entity.PageResponse;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.FeedRecordMapper;
import com.shaw.workrecord.pojo.request.FeedAddRequest;
import com.shaw.workrecord.pojo.request.FeedEditRequest;
import com.shaw.workrecord.pojo.request.FeedRecordPageRequest;
import com.shaw.workrecord.pojo.response.FeedResponse;
import com.shaw.workrecord.service.IFeedService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangjunhui
 * @description
 * @date 2023/8/10 14:16
 */
@Service
public class FeedServiceImpl extends ServiceImpl<FeedRecordMapper, FeedRecord> implements IFeedService {

    /**
     * 获取喂养喂养记录分页列表
     *
     * @param request 请求对象
     * @return
     */
    @Override
    public PageResponse<FeedResponse> getFeedRecordPage(FeedRecordPageRequest request) {
        LambdaQueryWrapper<FeedRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(FeedRecord::getChildrenId, request.getChildrenId()).orderByDesc(FeedRecord::getFeedTime);
        Page<FeedRecord> page = new Page<>(request.getPageIndex(), request.getPageSize());
        Page<FeedRecord> result = baseMapper.selectPage(page, wrapper);
        List<FeedResponse> collect = result.getRecords().stream().map(i -> {
            return BeanUtil.copyProperties(i, FeedResponse.class);
        }).collect(Collectors.toList());
        PageResponse<FeedResponse> recordResponsePage = new PageResponse<>();
        recordResponsePage.setRows(CollectionUtil.isEmpty(collect) ? new ArrayList<>() : collect);
        recordResponsePage.setTotal(result.getTotal());
        return recordResponsePage;
    }

    /**
     * 新增喂养记录
     *
     * @param request 请求对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addFeedRecord(FeedAddRequest request) {
        FeedRecord feedRecord = BeanUtil.copyProperties(request, FeedRecord.class);
        return save(feedRecord);
    }

    /**
     * 编辑喂养记录
     *
     * @param request 请求对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editFeedRecord(FeedEditRequest request) {
        FeedRecord feedRecord = BeanUtil.copyProperties(request, FeedRecord.class);
        return saveOrUpdate(feedRecord);
    }

    /**
     * 删除喂养记录
     *
     * @param id id
     * @return
     */
    @Override
    public Boolean deleteFeedRecord(Long id) {
        LambdaQueryWrapper<FeedRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(FeedRecord::getId).eq(FeedRecord::getId, id);
        FeedRecord feedRecord = baseMapper.selectOne(wrapper);
        if (feedRecord == null) {
            throw new CustomerException("不存在该数据");
        }
        return removeById(id);
    }
}
